package com.tjsinfo.tjsoa.system.bo;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.tjsinfo.tjsoa.Dao.IHibernateDao;
import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.system.dao.ITSystemBeiWangDao;
import com.tjsinfo.tjsoa.system.vo.TSystemBeiWang;
import com.tjsinfo.tjsoa.system.vo.TSystemUser;

public class TSystemBeiWangBo implements ITSystemBeiWangBo{

	private IQbcDao qbcDao;
	private IHibernateDao hibernateDao;
	public IHibernateDao getHibernateDao() {
		return hibernateDao;
	}

	public void setHibernateDao(IHibernateDao hibernateDao) {
		this.hibernateDao = hibernateDao;
	}

	public IQbcDao getQbcDao() {
		return qbcDao;
	}

	public void setQbcDao(IQbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}

	private ITSystemBeiWangDao tSystemBeiWangDao;
	
	public ITSystemBeiWangDao gettSystemBeiWangDao() {
		return tSystemBeiWangDao;
	}

	public void settSystemBeiWangDao(ITSystemBeiWangDao tSystemBeiWangDao) {
		this.tSystemBeiWangDao = tSystemBeiWangDao;
	}

	public void save(TSystemBeiWang systemBeiWang) {
		tSystemBeiWangDao.save(systemBeiWang);
		
	}

	public List<TSystemBeiWang> queryByUser(Integer userId, String start, String limit,String rq) {
		DetachedCriteria criteria = DetachedCriteria.forClass(TSystemBeiWang.class);
		TSystemUser systemUser = new TSystemUser();
		systemUser.setUserId(userId);
		criteria.add(Restrictions.eq("TSystemUser", systemUser));
		if(rq.equals("1")){
		}else{
			String time1 = rq+" 00:00";
			String time2 = rq+" 24:00";
			DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			try {
				criteria.add(Restrictions.between("datetime", format.parse(time1), format.parse(time2)));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		qbcDao.findPageByCriteria(criteria,Integer.parseInt(limit),Integer.parseInt(start));
		List<Object[]> listtemp = qbcDao.getCriteria(criteria);
		List<TSystemBeiWang> list = new ArrayList<TSystemBeiWang>();
		for(Object[] beiwangs : listtemp){
			list.add((TSystemBeiWang)beiwangs[1]);
		}
		return list;
	}

	public List<TSystemBeiWang> queryAll(Integer userId) {
//		DetachedCriteria criteria = DetachedCriteria.forClass(TSystemBeiWang.class);
//		criteria.add(Restrictions.eq("userId", userId));
//		return qbcDao.getCriteria(criteria);
		return tSystemBeiWangDao.queryAll(userId);
	}

	public void deleteBeiWang(String beizhu_uuid) {
		Integer uuid = Integer.parseInt(beizhu_uuid);
		TSystemBeiWang systemBeiWang = (TSystemBeiWang)hibernateDao.findById(TSystemBeiWang.class, uuid);
		hibernateDao.delete(systemBeiWang);
	}

	public TSystemBeiWang queryByUuid(String uuid) {
		DetachedCriteria criteria = DetachedCriteria.forClass(TSystemBeiWang.class);
		criteria.add(Restrictions.eq("uuid", uuid));
		return (TSystemBeiWang)qbcDao.getCriteria(criteria).iterator().next();
	}

	public void updateBeiwang(String date, String message,String id) {
		tSystemBeiWangDao.update(date, message, id);
		
	}

    
	
}
